﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ImageProcessing
{
	public partial class AffineDialog : Form
	{
		private Bitmap bmIn;
		private Bitmap bmBaseTh;
		private Bitmap bmModTh;
		private Bitmap bmOut;
		private OperationType optype;

		public Bitmap ImageOut
		{
			get { return bmOut; }
			set { bmOut = new Bitmap(value); }
		}

		public OperationType OpType
		{
			get { return optype; }
			set { optype = value; }
		}

		public AffineDialog()
		{
			InitializeComponent();
		}

		public AffineDialog(Bitmap bmin, OperationType op)
		{
			InitializeComponent();

			bmIn = new Bitmap(bmin);
			ImageOut = new Bitmap(bmin);

			bmModTh = (Bitmap)bmIn.GetThumbnailImage(200, 200, null, IntPtr.Zero);
			bmBaseTh = (Bitmap)bmIn.GetThumbnailImage(200, 200, null, IntPtr.Zero);
			OpType = op;

			picBoxBase.Invalidate();
			picBoxModified.Invalidate();
			
			switch (OpType)
			{
				case OperationType.Affine:
					break;
				case OperationType.Enhancement:
					break;
				default:
					break;
			}
		}

		//
		//Affine
		//
		private void btnRotate_Click(object sender, EventArgs e)
		{
			double angle;
			try
			{
				angle = Convert.ToDouble(txtboxRotateAngle.Text);
			}
			catch (Exception)
			{
				txtboxRotateAngle.SelectedText = "";
				return;
			}

			bmModTh = Affine.rotateImage(bmBaseTh, angle);
			bmModTh = (Bitmap)bmModTh.GetThumbnailImage(200, 200, null, IntPtr.Zero);
			picBoxModified.Invalidate();
			//ImageOut = Affine.rotateImage(bmIn, angle);
		}

		private void btnShear_Click(object sender, EventArgs e)
		{
			double angle;
			try
			{
				angle = Convert.ToDouble(txtboxShearAngle.Text);
			}
			catch (Exception)
			{
				txtboxShearAngle.Text = "";
				return;
			}

			bmModTh = Affine.shearImage(bmBaseTh, angle);
			bmModTh = (Bitmap)bmModTh.GetThumbnailImage(200, 200, null, IntPtr.Zero);
			picBoxModified.Invalidate();

			ImageOut = Affine.shearImage(bmIn, angle);
		}

		private void btnScale_Click(object sender, EventArgs e)
		{
			double dScaleY;
			double dScaleX;

			try
			{
				dScaleX = Convert.ToDouble(txtboxZoomFactor.Text);
				dScaleY = dScaleX;
			}
			catch (Exception)
			{
				txtboxZoomFactor.Text = "";
				return;
			}

			bmModTh = Affine.scaleImage(bmBaseTh, dScaleX,dScaleY);
			bmModTh = (Bitmap)bmModTh.GetThumbnailImage(200, 200, null, IntPtr.Zero);
			picBoxModified.Invalidate();

			ImageOut = Affine.scaleImage(bmIn, dScaleX,dScaleY);
		}



		private void picBoxModified_Paint(object sender, PaintEventArgs e)
		{
			picBoxModified.BackgroundImage = bmModTh;
		}

		private void picboxBase_Paint(object sender, PaintEventArgs e)
		{
			picBoxBase.BackgroundImage = bmBaseTh;
		}



		//
		//UI related events
		//
		private void grpboxRotate_Enter(object sender, EventArgs e)
		{
			this.AcceptButton = btnRotate;
		}
		private void grpboxShear_Enter(object sender, EventArgs e)
		{
			this.AcceptButton = btnShear;
		}
		private void grpboxZoom_Enter(object sender, EventArgs e)
		{
			this.AcceptButton = btnScale;
		}

		private void btnCommit_Click(object sender, EventArgs e)
		{
			bmBaseTh = new Bitmap(bmModTh);
			picBoxBase.Invalidate();
		}




		//
		//helpers
		//




	}
}
